from django.db import models
from django.utils import timezone
# 定义类
class Product(models.Model):
    # 产品名称
    product_name = models.CharField(max_length=21,unique=True,verbose_name='产品')
    # 商品封面
    product_cover = models.ImageField(upload_to='images',null=True,blank=True,verbose_name='产品封面')
    # 商品价格
    product_price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='商品价格')
    # 产品热度
    product_popularity = models.IntegerField(default=100,verbose_name='产品热度')
    # # 产品库存
    product_stock = models.IntegerField(default=100,verbose_name='产品库存')
    # # 产品状态
    is_active = models.BooleanField(default=False)
    # # 商品创建时间
    product_create_time = models.DateTimeField(default=timezone.now)
    # # 商品修改时间
    product_update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "product"
        verbose_name = '商品'
        verbose_name_plural = verbose_name
# Create your models here.
    # 增加返回的对象
    def __str__(self):
        return self.product_name
# 店铺评论模型 两张表关联起来，外键关联
class Review(models.Model):
    product = models.ForeignKey(Product,on_delete=models.CASCADE,verbose_name='关联的商品')
    username = models.CharField(max_length=21,verbose_name='用户名')
    avater = models.ImageField(upload_to='review_suser',verbose_name='用户头像')
    comment = models.TextField(verbose_name='评论内容')
    link_count = models.IntegerField(verbose_name='点赞量')
    create_time = models.DateTimeField(default=timezone.now,verbose_name='评论时间')
    upload_time = models.DateTimeField(auto_now=True,verbose_name='修改时间')    
    
    class Meta:
        db_table = '评论'       
        verbose_name = '评论'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.comment

